package org.dkakunsi.aplikasitoko.service;

import java.util.List;

import org.dkakunsi.aplikasitoko.data.entity.*;
import org.dkakunsi.aplikasitoko.data.util.ApplicationDataException;
import org.dkakunsi.aplikasitoko.data.util.HibernateUtil;
import org.dkakunsi.aplikasitoko.data.access.*;
import org.dkakunsi.aplikasitoko.data.adapter.*;
import org.hibernate.Session;

public class DataService {
	private GeneralDataAccess gda;
	private Session session;
	
	public DataService() { }
	
	public void save( Object object ) throws ApplicationDataException {
		restart();
		
		if( object instanceof Barang ) {
			gda = new BarangDataAdapter( session );
		} else if( object instanceof Kasir ) {
			gda = new KasirDataAdapter( session );
		} else if( object instanceof TransaksiBarang ) {
			gda = new TransaksiBarangDataAdapter(session);
		} else if( object instanceof Penjualan || object instanceof Pembelian ) {
			gda = new TransaksiDataAdapter( session );
		} else if( object instanceof Log || object instanceof Supplier ) {
			gda = new PelakuDataAdapter( session );
		} else { }
		
		try {
			gda.save( object );
		} catch( ApplicationDataException exc ) {
			throw exc;
		} finally {
			stop();
		}
	}
	
	public void update( Object object ) throws ApplicationDataException {
		restart();

		if( object instanceof Barang ) {
			gda = new BarangDataAdapter( session );
		} else if( object instanceof Kasir ) {
			gda = new KasirDataAdapter( session );
		} else if( object instanceof TransaksiBarang ) {
			gda = new TransaksiBarangDataAdapter( session );
		} else if( object instanceof Penjualan || object instanceof Pembelian ) {
			gda = new TransaksiDataAdapter( session );
		} else if( object instanceof Log || object instanceof Supplier ) {
			gda = new PelakuDataAdapter( session );
		}
		
		try {
			gda.update( object );			
		} catch( Exception exc ){
			throw new ApplicationDataException( exc.getMessage() );
		} finally {
			stop();
		}
	}
	
	public void delete( Object object ) throws ApplicationDataException {
		restart();

		if( object instanceof Barang ) {
			gda = new BarangDataAdapter( session );
		} else if( object instanceof Kasir ) {
			gda = new KasirDataAdapter( session );
		} else if( object instanceof TransaksiBarang ) {
			gda = new TransaksiBarangDataAdapter( session );
		} else if( object instanceof Penjualan || object instanceof Pembelian ) {
			gda = new TransaksiDataAdapter( session );
		} else if( object instanceof Log || object instanceof Supplier ) {
			gda = new PelakuDataAdapter( session );
		}
		
		try {
			gda.delete( object );			
		} catch( Exception exc ){
			throw new ApplicationDataException( exc.getMessage() );
		} finally {
			stop();
		}
	}
	
	public Object getInstance( String id, String type ) throws ApplicationDataException {
		restart();

		if( type.equals( "BARANG" ) ) {
			gda = new BarangDataAdapter( session );
		} else if( type.equals( "KASIR" ) ) {
			gda = new KasirDataAdapter( session );
		} else if( type.equals( "TRANSAKSI BARANG" ) ) {
			gda = new TransaksiBarangDataAdapter( session );
		} else if( type.equals( "TRANSAKSI" ) ) {
			gda = new TransaksiDataAdapter( session );
		} else if( type.equals( "PELAKU" ) ) {
			gda = new PelakuDataAdapter( session );
		}
		
		try {
			return gda.getByID( id );
		} catch( ApplicationDataException exc ){
			throw exc;
		} finally {
			stop();
		}
	}
	
	public Object getBy( String param, String type ) throws ApplicationDataException {
		restart();

		try {
			if( type.equals( "BARANG" ) ) {
				gda = new BarangDataAdapter( session );
				return gda.getBy( param );
			} else if( type.equals( "KASIR" ) ) {
				//gda = new KasirDataAdapter( session );
				return null;
			} else if( type.equals( "TRANSAKSI BARANG" ) ) {
				//gda = new TransaksiBarangDataAdapter( session );
				return null;
			} else if( type.equals( "TRANSAKSI" ) ) {
				//gda = new TransaksiDataAdapter( session );
				return null;
			} else if( type.equals( "PELAKU" ) ) {
				//gda = new PelakuDataAdapter( session );
				return null;
			} else {
				return null;
			}
		} catch( Exception exc ){
			throw new ApplicationDataException( exc.getMessage() );
		} finally {
			stop();
		}
	}
	
	@SuppressWarnings("rawtypes")
	public List getAll( String type ) throws ApplicationDataException {
		restart();

		try {
			if( type.equals( "BARANG" ) ) {
				BarangDataAccess barang = new BarangDataAdapter( session );
				return barang.getAll();
			} else if( type.equals( "KASIR" ) ) {
				KasirDataAccess kasir = new KasirDataAdapter( session );
				return kasir.getAll();
			} else if( type.equals( "TRANSAKSI BARANG" ) ) {
				TransaksiBarangDataAccess tb = new TransaksiBarangDataAdapter( session );
				return tb.getAll();
			} else if( type.equals( "TRANSAKSI" ) || type.equals( "PEMBELIAN" ) || type.equals( "PENJUALAN" )) {
				TransaksiDataAccess transaksi = new TransaksiDataAdapter( session );
				return transaksi.getAll( type );
			} else if( type.equals( "PELAKU" ) || type.equals( "LOG" ) || type.equals( "SUPPLIER" ) ) {
				PelakuDataAccess log = new PelakuDataAdapter( session );
				return log.getAll( type );
			} else {
				return null;
			}
		} catch( Exception exc ){
			throw new ApplicationDataException( exc.getMessage() );
		} finally {
			stop();
		}
	}
	
	public void attach( Object object ) {
		restart();

		session.beginTransaction();
		session.update(object);
		session.getTransaction().commit();
	}
	
	public Session getSession(){
		return this.session;
	}

	private boolean isStarted(){
		return session.isOpen();
	}
	
	public void stop() {
		if( isStarted() )
			session.close();
	}
	
	private void restart() {
		if(session != null){
			if( !isStarted() )
				this.session = HibernateUtil.openSession();
		} else {
			this.session = HibernateUtil.openSession();
		}
	}
	
	public void initialize() {
		this.session = HibernateUtil.openSession();
		HibernateUtil.close(session);
	}

}
